import axios from "axios";
import router from "@/router/index.js";
import Vue from "vue";

const BASE_URL = "http://localhost:8081/";

const api = axios.create({
	baseURL: BASE_URL,
	withCredentials: false,
	timeout: 50000000
})
// 拦截器配置，如果失败就不报错，由自己逻辑处理错误的情况
api.interceptors.response.use((response) => {
	return response;
}, (err) => {
	return err.response;
})
/**
 * 如何处理token
 */
async function tokenHandle(headers) {
	headers['Authorization'] = "token";
}
/**
 * 封装post请求
 * @param url 地址
 * @param params 参数
 * @param warning 是否警告
 * @param headers header
 * @param tokenNeed 是否需要token
 * @returns {Promise<any>}
 */
api.doPost = async function(url, params, warning = true, headers = {}, tokenNeed = true) {
	if (tokenNeed) {
		await tokenHandle(headers);
	}
	let response = await api.post(url, params, {
		headers
	});
	return response.data;
}
/**
 * 封装get请求
 * @param url 地址
 * @param params 参数
 * @param warning 是否警告
 * @param headers header
 * @param tokenNeed 是否需要token
 * @returns {Promise<any>}
 */
api.doGet = async function(url, params = {}, warning = true, headers = {}, tokenNeed = true) {
	if (tokenNeed) {
		await tokenHandle(headers);
	}
	let response = await api.get(url, {
		params,
		headers
	});
	return response.data;
}
export default {
	account: {
		login: (data) => api.doPost('/account/login', data)
	},
	sale: {
		// 修改路径为 /api/salesLocations，与后端保持一致
		getSalesLocations: (params) => api.doPost('/sale/salesLocations', params),
		delete: (data) => api.doPost('/sale/delete', data),
		update: (data) => api.doPost('/sale/update', data),
		add: (data) => api.doPost('/sale/add', data),
	},
	doctor: {
		getAllDoctors: (data) => api.doPost('/doctor/doctorsLocations', data),
		delete: (data) => api.doPost('/doctor/delete', data),
		getAllDoctorLevels: (data) => api.doPost('/doctor/doctorslevel', data),
		getAllDoctorTypes: (data) => api.doPost('/doctor/TreatType', data),
		update: (data) => api.doPost('/doctor/update', data),
		updatePassword: (data) => api.doPost('/doctor/updatepwd', data),
		add: (data) => api.doPost('/doctor/add', data),
	},
	company: {
		getCompanies: (params) => api.doPost('/company/companysLocations', params),
		delete: (data) => api.doPost('/company/delete', data),
		update: (data) => api.doPost('/company/update', data),
		add: (data) => api.doPost('/company/add', data),
	},
	drug:{
		getDrugs: (params) => api.doPost('/drug/drugsLocations', params),
		delete: (data) => api.doPost('/drug/delete', data),
		update: (data) => api.doPost('/drug/update', data),
		add: (data) => api.doPost('/drug/add', data),
	},
	city:{
		getStats:() => api.doGet('/api/dashboard/stats'),  // 补充 /api 前缀
		  getDoctorLevelStats:() => api.doGet('/api/dashboard/levelcount'), 
	},
	uploadresult:{
			upload: (file) => {
			      const formData = new FormData();
			      formData.append('file', file);
			      return api.doPost('/common/uploadminio', formData, {
			        headers: {
			          'Content-Type': 'multipart/form-data'
			        }
			      });
			    },
	},
};